//
//  NTNetUtil.m
//  SGShow
//
//  Created by fanshijian on 15-3-10.
//  Copyright (c) 2015年 fanshijian. All rights reserved.
//

#import "NTNetUtil.h"

@implementation NTNetUtil

/**
 * 数据请求
 **/
+ (void)sendRequestWithUrl:(NSString *)url
               requestName:(NSString *)requestName
                    method:(NTRequestMethod)methodType
                parameters:(id)parameters
                     block:(void (^)(NTHttpRequest *networkRequest, NSError *error))block {
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        @autoreleasepool {
            NTHttpRequest *request = [[NTHttpRequest alloc] init];
            [request executeHttpRequest:url requestName:requestName method:methodType params:parameters block:block];
        }
    });
}

/**
 * upload数据请求
 **/
+ (void)uploadRequestWithUrl:(NSString *)url
                 requestName:(NSString *)requestName
                      method:(NTRequestMethod)methodType
                  parameters:(id)parameters
                        data:(NSData *)data
                       block:(void (^)(NTHttpRequest *networkRequest, NSError *error))block {
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        @autoreleasepool {
            NTHttpRequest *request = [[NTHttpRequest alloc] init];
            [request uploadHttpRequest:url requestName:requestName method:methodType params:parameters data:data block:block];
        }
    });
}
+(void)postImgWithParam:(NSDictionary*)dic requestUrl:(NSString*)url dataArr:(NSArray*)arr block:(void (^)(NTHttpRequest *networkRequest, NSError *error))block
{
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        @autoreleasepool {
            NTHttpRequest *request = [[NTHttpRequest alloc] init];
            [request postImgWithParam:dic requestUrl:url dataArr:arr block:block];
        }
    });
}
+ (void)reLoginNum:(NSString *)phoneNum psw:(NSString *)psw complate:(void (^)(BOOL success))block {
    NSString *url = [NSString stringWithFormat:Login,phoneNum,psw,[Utils md5SignStr],[ShopCommonModel getShopMess].shopuser];
    [self sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSDictionary *userMess = dic[@"d"];
            [AppData share].cardno = userMess[@"cardno"];
            [AppData share].email = userMess[@"email"];
            [AppData share].img = userMess[@"img"];
            [AppData share].mobile = userMess[@"mobile"];
            [AppData share].realname = userMess[@"realname"];
            [AppData share].username = userMess[@"username"];
            [AppData share].usertype = userMess[@"usertype"];
            [AppData share].usertypestr = userMess[@"usertypestr"];
            [AppData share].doLogin = YES;
            [AppData share].loginPhone = phoneNum;
            [AppData share].loginPsw = psw;
            [[AppData share].baseTabVC theShoppingCartCount:userMess[@"shopcart"]];
            //登陆成功后 获取用户信息
            [self getUserMess];
            block(YES);
        }else {
            [[AppData share] clearValue];
            block(NO);
        }
    }];
}
#pragma mark - 获取个人信息
+ (void)getUserMess {
    NSString *url = [NSString stringWithFormat:GetUserMess,[AppData share].username,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:GetUserMess method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *suc = dic[@"r"];
        if ([suc isEqualToString:@"T"]) {
            NSDictionary *detail = dic[@"d"];
            NSString *noReadCount = detail[@"numunrradmessage"];
            [AppData share].baseTabVC.redLable.hidden = [noReadCount integerValue] == 0?YES:NO;
        }else {
            [[MessShowView share] showWithMess:dic[@"m"]];
        }
    }];
}
@end


